<?php
class Search extends Zend_Form {
	
	private $fromCountryId;
	private $toCountryId;
	private $backUrl;
	public function Search($fromCountryId=null, $toCountryId=null, $backUrl=null) {
		$this->fromCountryId = $fromCountryId;
		$this->toCountryId = $toCountryId;
		$this->backUrl = $backUrl;
		$this->__construct();
	}
	
	public function init() {
		$config = Zend_Registry::get("config");
		$this->setName("SearchForm");
		$this->setAction($config->baseurl."/search/searchlist");
		$searchData = SessionUtil::getProperty(Constant::SEARCH_DATA);
		$this->setMethod('POST');
		$this->setName('searchForm');
		
		if (!empty($this->backUrl)) {
			$element = new Zend_Form_Element_Hidden("backUrl");
			$element->setValue($this->backUrl);
			$this->addElement($element);
		}
		
		$element = new Zend_Form_Element_Radio('type');
		$element->addMultiOptions(
			array(
				'all' => '全部',
				'lease' => '可托',
				'want' => '求托'
			)
		);
		if (!empty($searchData[Constant::VAR_SEARCH_DATA_TYPE])) {
			$element->setValue($searchData[Constant::VAR_SEARCH_DATA_TYPE]);
		} else {
			$element->setValue('all');	
		}
		$element->setSeparator('');
		$element->removeDecorator('Label');
		$this->addElement($element);
		
		
//		$element = new Zend_Form_Element_Checkbox("search_business");
//		$element->setValue(0);
//		$this->addElement($element);
		
		$element = new Zend_Form_Element_Hidden(Constant::VAR_SEARCH_DATA_STATUS);
		$element->setValue('active');
		$this->addElement($element);
		
		
		$element = new Zend_Form_Element_Select(Constant::VAR_SEARCH_DATA_FROM_COUNTRY);
		$element->removeDecorator('Label');
        $element->addMultiOption(0, "- 出发国家 -");
        $element->addMultiOptions(Country::getAvailableCountriesAsArray());
        if (!empty($searchData[Constant::VAR_SEARCH_DATA_FROM_COUNTRY])) {
        	$element->setValue($searchData[Constant::VAR_SEARCH_DATA_FROM_COUNTRY]);
        }
        $this->addElement($element);
		
        $element = new Zend_Form_Element_Select(Constant::VAR_SEARCH_DATA_FROM_CITY);
		$element->removeDecorator('Label');
        $element->addMultiOption(0, "- 出发城市 -");
        if (!empty($searchData[Constant::VAR_SEARCH_DATA_FROM_COUNTRY])) {
	        $element->addMultiOptions(Country::getCityByCountryAsArray($searchData[Constant::VAR_SEARCH_DATA_FROM_COUNTRY]));
			if (!empty($searchData[Constant::VAR_SEARCH_DATA_FROM_CITY])) {
				$element->setValue($searchData[Constant::VAR_SEARCH_DATA_FROM_CITY]);
			}
        }
        $this->addElement($element);
        
        
		$this->addDisplayGroup(array('from_country','from_city'),'from_place');
		$this->getDisplayGroup('from_place')->removeDecorator('DtDdWrapper');
		
        $element = new Zend_Form_Element_Select(Constant::VAR_SEARCH_DATA_TO_COUNTRY);
		$element->removeDecorator('Label');
        $element->addMultiOption(0, "- 抵达国家 -");
        $element->addMultiOptions(Country::getAvailableCountriesAsArray());
		if (!empty($searchData[Constant::VAR_SEARCH_DATA_TO_COUNTRY])) {
        	$element->setValue($searchData[Constant::VAR_SEARCH_DATA_TO_COUNTRY]);
        }
        $this->addElement($element);
        
        $element = new Zend_Form_Element_Select(Constant::VAR_SEARCH_DATA_TO_CITY);
		$element->removeDecorator('Label');
        $element->addMultiOption(0, "- 抵达城市 -");
        if (!empty($searchData[Constant::VAR_SEARCH_DATA_TO_COUNTRY])) {
	        $element->addMultiOptions(Country::getCityByCountryAsArray($searchData[Constant::VAR_SEARCH_DATA_TO_COUNTRY]));
			if (!empty($searchData[Constant::VAR_SEARCH_DATA_TO_CITY])) {
				$element->setValue($searchData[Constant::VAR_SEARCH_DATA_TO_CITY]);
			}
        }
        $this->addElement($element);
        
		$this->addDisplayGroup(array('to_country','to_city'),'to_place');
		$this->getDisplayGroup('to_place')->removeDecorator('DtDdWrapper');
		
		$element = new Zend_Form_Element_Text(Constant::VAR_SEARCH_DATA_LASTEST_ARRIVAL_DATE);
		$element->setLabel('最晚抵达时间');
		if (!empty($searchData[Constant::VAR_SEARCH_DATA_LASTEST_ARRIVAL_DATE]) && 
			$searchData[Constant::VAR_SEARCH_DATA_LASTEST_ARRIVAL_DATE] != Constant::VAR_SEARCH_DATA_LASTEST_ARRIVAL_DATE) {
			$element->setValue($searchData[Constant::VAR_SEARCH_DATA_LASTEST_ARRIVAL_DATE]);
		}
		$this->addElement($element);
				
		$element = new Zend_Form_Element_Submit('search');
		$element->setValue('搜索');
		$element->removeDecorator('Label');
		$this->addElement($element);
		
		$element = new Zend_Form_Element_Submit('reset');
		$element->setValue('清空');
		$element->removeDecorator('Label');
		$this->addElement($element);
	}
}
?>